package com.mzw.huaxiarealestatebackend.controller;

import com.baomidou.mybatisplus.extension.api.R;
import com.mzw.huaxiarealestatebackend.common.utils.DataResult;
import com.mzw.huaxiarealestatebackend.common.utils.DateFormat;
import com.mzw.huaxiarealestatebackend.entity.*;
import com.mzw.huaxiarealestatebackend.service.*;
import io.swagger.models.auth.In;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.xml.crypto.Data;
import java.security.PrivateKey;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;
import java.util.List;

/**
 * (ProjectRecord)表控制层
 *
 * @author makejava
 * @since 2023-12-22 00:03:04
 */
@RestController
@RequestMapping("projectRecord")
public class ProjectRecordController {
    /**
     * 服务对象
     */
    @Resource
    private ProjectRecordService projectRecordService;
    @Autowired
    private StaffService staffService;
    @Autowired
    private MailService mailService;
    @Autowired
    private ProjectService projectService;
    @Autowired
    private ClientService clientService;
    @Autowired
    private DepartmentService departmentService;


    /**
     * 上传文件
     * @param pjtNo
     * @param pcsNo
     * @return
     */
    @PutMapping("/editFileStatus")
    public DataResult editFileStatus(@RequestParam("pjtNo") String pjtNo, @RequestParam("pcsNo") String pcsNo){
        ProjectRecord pr = new ProjectRecord();
        pr.setPjtNo(pjtNo);
        pr.setPcsNo(Integer.parseInt(pcsNo));
        List<ProjectRecord> projectRecords = this.projectRecordService.queryByCondition(pr);
        if (projectRecords.size() == 0){
            return DataResult.failure("查找失败");
        }else {
            ProjectRecord projectRecord = projectRecords.get(0);
            projectRecord.setFileStatus("1");
            boolean update = this.projectRecordService.update(projectRecord);
            if (update) return DataResult.ok(update);
        }
        return DataResult.failure("上传失败");
    }


    /**
     * 查询每个阶段涉及到的人员以及项目状态
     * @param pjtNo
     * @param pcsNo
     * @return
     */
    @GetMapping("/queryObject")
    public DataResult queryObject(@RequestParam("pjtNo") String pjtNo, @RequestParam("pcsNo") String pcsNo ){
        Query query = new Query();

        ProjectRecord projectRecord = new ProjectRecord();
        projectRecord.setPcsNo(Integer.parseInt(pcsNo));
        projectRecord.setPjtNo(pjtNo);
        List<ProjectRecord> projectRecords = this.projectRecordService.queryByCondition(projectRecord);
        //员工所在部门
        Department department;
        if (!pcsNo.equals("5")){
            if (pcsNo.equals("1")){
                department = this.departmentService.queryById("2");
            }else {
                department = this.departmentService.queryById(pcsNo);
            }
            //主管信息
            String manageName = department.getDpCaptain();
            String manageTel = department.getDpTel();
            query.setAdminName(manageName);
            query.setAdminTel(manageTel);
        }
        if (projectRecords.size() != 0){
            ProjectRecord projectRecord1 = projectRecords.get(0);
            //设置此条项目记录的完成情况
            query.setFinishStatus(Integer.toString(projectRecord1.getPrcdFinishstatus()));
            //设置主管审核状态
            query.setManageCheckStatus(Integer.toString(projectRecord1.getPrcdMcheckstatus()));
            //设置客户审核状态
            query.setClientCheckStatus(Integer.toString(projectRecord1.getPrcdCcheckstatus()));
            //设置文件上传状态
            query.setFileStatus(projectRecord1.getFileStatus());
            //设置文件上传状态的名称
            if (projectRecord1.getFileStatus().equals("0")){
                query.setFileStatusName("未上传");
            }else {
                query.setFileStatusName("已上传");
            }
            //负责员工的id
            String stfNo = projectRecords.get(0).getStfNo();
            if (stfNo != null && !stfNo.equals("")){
                Staff staff = this.staffService.queryById(stfNo);
                query.setStaffName(staff.getStfName());
                query.setStaffTel(staff.getStfTel());
            }
            if (projectRecord1.getPrcdMcheckstatus() == 1 && projectRecord1.getPrcdCcheckstatus() == 1 && projectRecord1.getFileStatus().equals("1")){
                Project project = this.projectService.queryById(pjtNo);
                if (pcsNo.equals("4")){
                    project.setPjtFinishtime(DateFormat.getNowDateFormat());
                    project.setPjtStatus("1");
                }
                this.projectService.update(project);
                query.setIsChecked("审核通过");
            } else if ((projectRecord1.getPrcdMcheckstatus() != 1 || projectRecord1.getPrcdCcheckstatus() != 1) && projectRecord1.getFileStatus().equals("1")){
                query.setIsChecked("审核未通过");
            }else {
                query.setIsChecked("未审核");
            }
            if (projectRecord1.getPrcdFinishstatus() == 1){
                query.setIsFinished("已完成");
            }else {
                query.setIsFinished("未完成");
            }
        }
        return  DataResult.ok(query);
    }

    /**
     * 主管审核-同意
     * @param pcsNo
     * @param pjtNo
     * @return
     */
    @PutMapping("/acceptCheck")
    public DataResult acceptCheck(@RequestParam("pcsNo") String pcsNo, @RequestParam("pjtNo") String pjtNo){
        //更新项目记录
        ProjectRecord projectRecord = new ProjectRecord();
        projectRecord.setPjtNo(pjtNo);
        projectRecord.setPcsNo(Integer.parseInt(pcsNo));
        projectRecord.setFileStatus("1");
        List<ProjectRecord> projectRecords1 = this.projectRecordService.queryByCondition(projectRecord);
        if (projectRecords1.size() != 0){
            ProjectRecord projectRecord1 = projectRecords1.get(0);
            projectRecord.setPrcdNo(projectRecords1.get(0).getPrcdNo());
            projectRecord.setPrcdMcheckstatus(1);
            projectRecord.setPrcdCtime(DateFormat.getNowDateFormat());
            //如果主管和客户都满意进入下一阶段
            if (projectRecord1.getPrcdCcheckstatus() == 1){
                projectRecord.setPrcdFinishstatus(1);
                Project project = new Project();
                project.setPjtNo(projectRecord1.getPjtNo());
                project.setPjtStop(Integer.toString(Integer.parseInt(pcsNo) + 1));
                if (pcsNo.equals("4")){
                    project.setPjtStatus("1");
                }
                boolean update1 = this.projectService.update(project);
            }
            boolean update = this.projectRecordService.update(projectRecord);
            if (update) return DataResult.ok();
        }
        return DataResult.failure("审核失败");
    }

    /**
     * 客户验收-同意
     * @param pcsNo
     * @param pjtNo
     * @return
     */
    @PutMapping("/acceptCheckClient")
    public DataResult acceptCheckClient(@RequestParam("pcsNo") String pcsNo, @RequestParam("pjtNo") String pjtNo){
        if (pcsNo.equals("5")){
            Project p = new Project();
            p.setPjtStatus("1");
            p.setPjtNo(pjtNo);
            p.setPjtFinishtime(DateFormat.getNowDateFormat());
            p.setPjtStop("5");
            boolean update = this.projectService.update(p);
            if (update) return DataResult.ok("审核成功");
            return DataResult.failure("审核失败");
        }
        //更新项目记录
        //查询当前项目记录
        ProjectRecord projectRecord = new ProjectRecord();
        projectRecord.setPjtNo(pjtNo);
        projectRecord.setPcsNo(Integer.parseInt(pcsNo));
        projectRecord.setFileStatus("1");
        List<ProjectRecord> projectRecords1 = this.projectRecordService.queryByCondition(projectRecord);
        if (projectRecords1.size() != 0){
            ProjectRecord projectRecord1 = projectRecords1.get(0);
            projectRecord.setPrcdNo(projectRecords1.get(0).getPrcdNo());
            projectRecord.setPrcdCcheckstatus(1);
            projectRecord.setPrcdCtime(DateFormat.getNowDateFormat());
            //如果主管和客户都满意进入下一阶段
            if (projectRecord1.getPrcdMcheckstatus() == 1){
                projectRecord.setPrcdFinishstatus(1);
                Project project = new Project();
                project.setPjtNo(projectRecord1.getPjtNo());
                if (pcsNo.equals("4")){
                    project.setPjtStatus("1");
                }else {
                    project.setPjtStop(Integer.toString(Integer.parseInt(pcsNo) + 1));
                }
                boolean update1 = this.projectService.update(project);
            }
            boolean update = this.projectRecordService.update(projectRecord);
            if (update) return DataResult.ok();
        }
        return DataResult.failure("审核失败");
    }

    /**
     * 主管审核-不同意
     * @param pcsNo
     * @param pjtNo
     * @param reason
     * @param userId
     * @return
     */
    @PutMapping("/refuseCheck")
    public DataResult refuseCheck(@RequestParam("userId") String userId,@RequestParam("pcsNo") String pcsNo, @RequestParam("pjtNo") String pjtNo, @RequestParam("reason") String reason){
        //查询当前项目记录
        ProjectRecord projectRecord = new ProjectRecord();
        projectRecord.setPjtNo(pjtNo);
        projectRecord.setPcsNo(Integer.parseInt(pcsNo));
        projectRecord.setFileStatus("1");
        List<ProjectRecord> projectRecords1 = this.projectRecordService.queryByCondition(projectRecord);
        if (projectRecords1.size() != 0){
            ProjectRecord projectRecord1 = projectRecords1.get(0);
            //向员工发送邮件
            //当前主管
            Staff staff = this.staffService.queryById(userId);
            this.mailService.sendMail(userId, projectRecord1.getStfNo(), staff.getStfName(), reason);
            //删除、插入项目记录表
            projectRecord1.setPrcdCtime(DateFormat.getNowDateFormat());
            projectRecord1.setPrcdMcheckstatus(0);
            projectRecord1.setPrcdCcheckstatus(0);
            projectRecord1.setFileStatus("0");
            projectRecord1.setStfNo("");
            projectRecord1.setPrcdMtips(reason);
            //更改项目的停留阶段
            Project project = new Project();
            project.setPjtNo(projectRecord1.getPjtNo());
            project.setPjtStop(pcsNo);
            //更改项目的完成状态
            project.setPjtStatus("0");
            this.projectService.update(project);

            boolean b = this.projectRecordService.deleteById(projectRecord1.getPrcdNo());
            boolean insert = this.projectRecordService.insert(projectRecord1);
            if (insert) return DataResult.ok();
        }
        return DataResult.failure("审核失败");
    }

    /**
     * 客户审核-不同意
     * @param pcsNo
     * @param pjtNo
     * @param reason
     * @param userId
     * @return
     */
    @PutMapping("/refuseCheckClient")
    public DataResult Client(@RequestParam("userId") String userId,@RequestParam("pcsNo") String pcsNo, @RequestParam("pjtNo") String pjtNo, @RequestParam("reason") String reason){
        //查询当前项目记录
        ProjectRecord projectRecord = new ProjectRecord();
        projectRecord.setPjtNo(pjtNo);
        projectRecord.setPcsNo(Integer.parseInt(pcsNo));
        projectRecord.setFileStatus("1");
        List<ProjectRecord> projectRecords1 = this.projectRecordService.queryByCondition(projectRecord);
        if (projectRecords1.size() != 0){
            ProjectRecord projectRecord1 = projectRecords1.get(0);
            //向员工发送邮件
            //当前主管
            Client client = this.clientService.queryById(userId);
            this.mailService.sendMail(userId, projectRecord1.getStfNo(), client.getCltName(), reason);
            //删除、插入项目记录表
            projectRecord1.setPrcdCtime(DateFormat.getNowDateFormat());
            projectRecord1.setPrcdMcheckstatus(0);
            projectRecord1.setPrcdCcheckstatus(0);
            projectRecord1.setFileStatus("0");
            projectRecord1.setStfNo("");
            projectRecord1.setPrcdFinishstatus(0);
            projectRecord1.setPrcdCtips(reason);
            //更改项目的停留阶段
            Project project = new Project();
            project.setPjtNo(projectRecord1.getPjtNo());
            project.setPjtStop(pcsNo);
            this.projectService.update(project);
            //更改项目的完成状态
            project.setPjtStatus("0");
            //将pcsNo之后项目记录的置为0
            int phase = Integer.parseInt(pcsNo);
            for (int i = phase + 1; i < 5; i++){
                //查询i号当前项目记录
                ProjectRecord projectRecord2 = new ProjectRecord();
                projectRecord2.setPjtNo(pjtNo);
                projectRecord2.setPcsNo(i);
                List<ProjectRecord> projectRecords3 = this.projectRecordService.queryByCondition(projectRecord2);
                if (projectRecords3.size() != 0){
                    ProjectRecord projectRecord3 = projectRecords3.get(0);
                    projectRecord3.setStfNo("");
                    projectRecord3.setFileStatus("0");
                    projectRecord3.setPrcdCcheckstatus(0);
                    projectRecord3.setPrcdMcheckstatus(0);
                    projectRecord3.setFileStatus("0");
                    projectRecord3.setPrcdFinishstatus(0);
                    boolean b = this.projectRecordService.deleteById(projectRecord3.getPrcdNo());
                    if (b) {
                        boolean insert = this.projectRecordService.insert(projectRecord3);
                        if (insert) continue;
                    }
                }else {
                    break;
                }
            }
            boolean b = this.projectRecordService.deleteById(projectRecord1.getPrcdNo());
            boolean insert = this.projectRecordService.insert(projectRecord1);
            if (insert) return DataResult.ok();
        }
        return DataResult.failure("审核失败");
    }

    /**
     * 员工拒绝任务
     * @param pjtNo
     * @param pcsNo
     * @param reason
     * @param userId
     * @return
     */
    @DeleteMapping("/refuseTask")
    public DataResult refuseTask(@RequestParam("pjtNo")String pjtNo, @RequestParam("pcsNo") String pcsNo, @RequestParam("reason")String reason, @RequestParam("userId") String userId){
        ProjectRecord projectRecord = new ProjectRecord();
        projectRecord.setPjtNo(pjtNo);
        projectRecord.setPcsNo(Integer.parseInt(pcsNo));
        projectRecord.setStfNo(userId);
        List<ProjectRecord> projectRecords = this.projectRecordService.queryByCondition(projectRecord);
        if (projectRecords.size() == 0){
            return DataResult.failure("项目记录不存在");
        }
        //获取当前记录id
        String prcdNo = projectRecords.get(0).getPrcdNo();
        //找到当前项目记录
        ProjectRecord projectRecord1 = this.projectRecordService.queryById(prcdNo);
        //负责的员工no
        String stfNo = projectRecord1.getStfNo();
        //寻找主管no
        Staff staff = this.staffService.queryById(stfNo);
        String dpNo = staff.getDpNo();
        Staff staff1 = new Staff();
        staff1.setDpNo(dpNo);
        staff1.setStfLevel("1");
        List<Staff> staff2 = this.staffService.queryByCondition(staff1);
        String stfNoAdmin = staff2.get(0).getStfNo();

        this.mailService.sendMail(stfNo, stfNoAdmin, staff.getStfName(), reason);

        boolean b = this.projectRecordService.deleteById(prcdNo);
        if (b) {
            return DataResult.ok();
        }
        return DataResult.failure("拒绝失败");
    }


    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public DataResult queryById(@PathVariable("id") String id) {
        ProjectRecord projectRecord = this.projectRecordService.queryById(id);
        if (projectRecord != null){
            return DataResult.ok(projectRecord);
        }
        return DataResult.failure("查找失败");
    }

    /**
     * 新增数据
     *
     * @param projectRecord 实体
     * @return 新增结果
     */
    @PostMapping
    public DataResult add(@RequestBody ProjectRecord projectRecord) {
        boolean insert = this.projectRecordService.insert(projectRecord);
        if (insert){
            return DataResult.ok();
        }
        return DataResult.failure("添加失败");
    }

    /**
     * 编辑数据
     *
     * @param projectRecord 实体
     * @return 编辑结果
     */
    @PutMapping
    public DataResult edit(@RequestBody ProjectRecord projectRecord) {
        boolean update = this.projectRecordService.update(projectRecord);
        if (update){
            return DataResult.ok();
        }
        return DataResult.failure("修改失败");
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @DeleteMapping
    public DataResult deleteById(@RequestParam("id") String id) {
        boolean b = this.projectRecordService.deleteById(id);
        if (b){
            return DataResult.ok();
        }
        return DataResult.failure("删除失败");
    }

}

